﻿ #ifndef RULEBAR
#define RULEBAR

#include <QWidget>
#include <QPen>
#include <QMouseEvent>

QT_BEGIN_NAMESPACE
class QFrame;
class QPaintEvent;
class QPainter;
class QGraphicsView;
QT_END_NAMESPACE

namespace eintik::presentation::viewcore {

#define RULER_SIZE 32

enum class RotateType { R0, R90, R180, R270, V_Mirror };

// Right_Ruler1 color_bar
enum QRulerPos { Left_Ruler, Bottom_Ruler, Right_Ruler1, Right_Ruler2, Top_Ruler };

enum QRulerType {
    SoundPath_Ruler,
    Depth_Ruler,
    HDistance_Ruler, // index
    Height_Ruler,
    ColorBar_Ruler,
    Angle_Ruler,
    VPA_Ruler,
    Scan_Ruler,
    TOFD_Depth_Ruler, // TOFD 深度刻度尺
    Unknown_Ruler = 0xff
};

class QRuleBar : public QWidget
{
    Q_OBJECT

    static const QTransform gRotateForm90;
    static const QTransform gRotateForm270;

public:
    explicit QRuleBar(QRulerPos direction, QRulerType type, QGraphicsView *view,
                      QWidget *parent = nullptr);
    ~QRuleBar(void) Q_DECL_OVERRIDE;

    void setRange(double lower, double upper, double max_size);
    void getRange(double &lower, double &upper, double &max_size);
    void updatePosition(const QPoint &pos);

    double getTofdDepth(double timeUs);
    double getTofdTime(double depth);

    void setRulerEnable(bool);
    void setRulerPosition(QRulerPos);
    QRulerPos getRulerPosition();
    void setRulerType(QRulerType);
    void setUnitStr(const QString &);
    const QString getUnitStr(void) const;

    bool RulerEnable(void) const;

    void setZoomEnable(bool);
    bool getZoomEnalbe();

    void setZoomValue(double v1, double v2);
    void getZoomValue(double &v1, double &v2);

    void setBackGroudColor(QColor color);
    void setColorMap(const QVector<QColor> &colorVector);

    void setGateEnable(int32_t GateId, bool enable);
    void setGateHeight(int32_t GateId, int32_t GateHeight);
    void setGatePosition(int32_t GateId, double GateMark1, double GateMark2);

    void setPieceVelocity(double velocity);
    void setPCS(double pcs, double t0);

    void setTransform(const QTransform &);

    void setScanPath(bool radian, double d);

    RotateType rotateNo = RotateType::R0;

protected:
    void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
    QString getTickText(double cur, int scale, bool unit);
    bool getTofdDepth(double timeUs, double &tofdDepth);
    void drawTicker(QPainter *painter);
    void drawTofdTicker(QPainter *painter);
    void drawGatePos(QPainter *painter, const QRect &rect);

    // 绘制缩放指示框
    void drawZoomSign(QPainter *painter, int w, int h);

    //    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    virtual void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    virtual void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    virtual void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    virtual void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

    bool isMirror(void);

    //    Qt::Orientation   m_direction;
    QRulerPos m_direction;      // 没有旋转的时候刻度尺位置
    QRulerPos m_show_direction; // 刻度尺实际显示位置
    QRulerType m_type;
    QPoint m_lastPos;
    QColor m_faceColor;
    QGraphicsView *m_view;

    double m_lower;
    double m_upper;
    double m_maxsize;

signals:
    void signalRefeshGrid(const QVector<double> &GridInfo, bool isVertical);
    void signalPopPalette(void);
    void signalZoomChanged(double s, double e);
    void signalZoomFinished();

private:
    bool m_enable;
    enum MoveType { MoveBegin, MoveEnd, Move, None };

    //    uint8_t color_r_map[260], color_g_map[260], color_b_map[260];
    QVector<QColor> colorMap;

    /* 绘制闸门在刻度尺上的指示标志 */
    bool m_GateEnable[5];
    bool zoomEnable = false; // 是否提供放大功能
    bool zooming = false;    // 正在放大中
    bool radian = false;

    int32_t m_GateHeight[5];
    int32_t m_GateMark1[5]; // 起点的百分比
    int32_t m_GateMark2[5]; // 终点点的百分比

    int32_t p1;
    MoveType moveType = MoveType::None;
    double moveBeginPos;
    double zoomStart = 0.0;
    double zoomEnd = 1.0;
    double lastZoomStart = 0.0;
    double lastZoomEnd = 1.0;

    QVector<double> m_VGridInfo;

    QString m_unit;

    double pieceVelocity = 5920.0;
    double tofdPcs = 100.0;
    double wedgeDelay = 1.0;

    QTransform rotateForm = QTransform(1, 0, 0, 0, 1, 0, 0, 0, 1);

    double diameter = 10;
};

}

#endif // RULEBAR
